package com.weirong.smartvault.backservice.controller;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.weirong.smartvault.common.tools.constant.APIResult;
import com.weirong.smartvault.dao.*;
import com.weirong.smartvault.entity.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.lang.reflect.Type;
import java.util.*;

/**
 * 于2017/12/3由 KFEB4 创建 .
 * 增删改查的操作
 */
@Controller
@RequestMapping("/API")
public class API {
    @Autowired
    DepartmentRepository departmentRepository;

    @Autowired
    DeliveryLogRepository deliveryLogRepository;

    @Autowired
    ForeignCurrencyRequestRepository foreignCurrencyRequestRepository;
    @Autowired
    ForeignCurrencyRequestDetailRepository foreignCurrencyRequestDetailRepository;

    @Autowired
    InStorageLogRepository inStorageLogRepository;

    @Autowired
    InStorageDetailLogRepository inStorageDetailLogRepository;

    @Autowired
    CashboxUnitRepository  cashboxUnitRepository;

    @Autowired
    ForeignCurrencyTypeRepository foreignCurrencyTypeRepository;

    @Autowired
    ForeignCurrencyDenominationRepository foreignCurrencyDenominationRepository;
    @Autowired
    DeliveryDetailLogRepository deliveryDetailLogRepository;

    @Autowired
    StockHistoryRepository stockHistoryRepository;

    @Autowired
    AuthorityRepository authorityRepository;
    @Autowired
    RoleRepository roleRepository;
    @RequestMapping("/delete_role")
    public String deleteRole(@RequestParam(value = "id",defaultValue = "-1")Integer id){
        roleRepository.delete(id);
        return APIResult.success;
    }

    @RequestMapping("/departmentDelete")
    @ResponseBody
    public String deleteDepartment(@RequestParam(value = "id", defaultValue = "-1") Integer id) {
        if (id < 0)
            return APIResult.fail;
        else {
            departmentRepository.delete(id);
        }
        return APIResult.success;
    }

    @RequestMapping("/cashUnitDelete")
    @ResponseBody
    public String deleteCashUnit(@RequestParam(value = "id", defaultValue = "-1") Integer id){
        if (id<0)
            return APIResult.fail;
        else
            cashboxUnitRepository.delete(id);
        return APIResult.success;
    }
    @RequestMapping("/foreignCurrencyTypeDelete")
    @ResponseBody
    public String deleteForeignCurrencyType(@RequestParam(value = "id", defaultValue = "-1") Integer id){
        if (id<0)
            return APIResult.fail;
        else
            foreignCurrencyTypeRepository.delete(id);
        return APIResult.success;
    }

    @RequestMapping("/denominationDelete")
    @ResponseBody
    public String deleteDenomination(@RequestParam(value = "id",defaultValue = "-1") Integer id){
        if (id<0)
            return APIResult.fail;
        else
            foreignCurrencyDenominationRepository.delete(id);
        return APIResult.success;
    }

    @RequestMapping(value = "/set_role_permission",method = RequestMethod.POST)
    @ResponseBody
    public String setRolePermission(@RequestParam(value = "id",defaultValue = "-1") Integer id,@RequestBody String info){
        Role role =  roleRepository.findOne(id);
        Gson gson = new Gson();
        Type type = new TypeToken<ArrayList<Integer>>() {
        }.getType();
        List<Integer> data = gson.fromJson(info, type);
        List<Authority> authorities= authorityRepository.findAllByIdIn(data);
        role.setAuthorities(new HashSet<>(authorities));
        roleRepository.save(role);
        return APIResult.success;
    }


}
